# 店铺信息
from django.shortcuts import render
from django.http import HttpResponse

from datetime import datetime
import time

from myadmin.models import Shop
from myadmin.utils import get_page, params_status


def index(request, pIndex=1):
    slist, mywhere, kw = params_status(Shop, request)

    if kw:
        slist = slist.filter(name__contains=kw)
        mywhere.append('keyword=' + kw)

    slist = slist.order_by('id')
    list2, plist, pIndex, maxpages = get_page(slist, pIndex, 5)
    context = {"shoplist": list2, 'plist': plist, 'pIndex': pIndex, 'maxpages': maxpages, 'mywhere': mywhere}
    return render(request, "myadmin/shop/index.html", context)


def add(request):
    return render(request, "myadmin/shop/add.html")


def insert(request):
    try:
        myfile = request.FILES.get("cover_pic", None)
        if not myfile:
            return HttpResponse("没有店铺封面上传文件信息")
        cover_pic = str(time.time()) + "." + myfile.name.split('.').pop()
        destination = open("./static/uploads/shop/" + cover_pic, "wb+")
        for chunk in myfile.chunks():
            destination.write(chunk)
        destination.close()

        myfile = request.FILES.get("banner_pic", None)
        if not myfile:
            return HttpResponse("没有店铺logo上传文件信息")
        banner_pic = str(time.time()) + "." + myfile.name.split('.').pop()
        destination = open("./static/uploads/shop/" + banner_pic, "wb+")
        for chunk in myfile.chunks():
            destination.write(chunk)
        destination.close()

        ob = Shop()
        ob.name = request.POST['name']
        ob.address = request.POST['address']
        ob.phone = request.POST['phone']
        ob.cover_pic = cover_pic
        ob.banner_pic = banner_pic
        ob.status = 1
        ob.create_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        ob.update_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        ob.save()
        context = {'info': "添加成功！"}
    except Exception:
        context = {'info': "添加失败！"}
    return render(request, "myadmin/info.html", context)


def delete(request, sid=0):
    '''执行信息删除'''
    try:
        ob = Shop.objects.get(id=sid)
        ob.status = 9
        ob.update_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        ob.save()
        context = {'info': "删除成功！"}
    except Exception:
        context = {'info': "删除失败！"}
    return render(request, "myadmin/info.html", context)


def edit(request, sid=0):
    try:
        ob = Shop.objects.get(id=sid)
        context = {'shop': ob}
        return render(request, "myadmin/shop/edit.html", context)
    except Exception:
        context = {'info': "没有找到要修改的信息！"}
        return render(request, "myadmin/info.html", context)


def update(request, sid):
    try:
        ob = Shop.objects.get(id=sid)
        ob.name = request.POST['name']
        ob.address = request.POST['address']
        ob.phone = request.POST['phone']
        ob.status = request.POST['status']
        ob.update_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        ob.save()
        context = {'info': "修改成功！"}
    except Exception:
        context = {'info': "修改失败！"}
    return render(request, "myadmin/info.html", context)
